package org.example.service.impl;

import org.example.mapper.NotifyMapper;
import org.example.pojo.Notifies;
import org.example.pojo.NotifiesDTO;
import org.example.utils.R;
import org.example.service.NotifyService;
import org.example.utils.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @Description: service实现层
 * @Author:曹震
 * @Date: 2024/12/1 17:35
 */
@Service
public class NotifyServiceImpl implements NotifyService {
    @Resource
    private NotifyMapper mapper;
    //发布通知
    @Override
    public void addNotify(NotifiesDTO notifyDto) {
        Notifies notifies = new Notifies();
        notifies.setTitle(notifyDto.getTitle());
        notifies.setContent(notifyDto.getContent());
        notifies.setBookDate(notifyDto.getBookData());
        notifies.setStatus(1);
        notifies.setCreateDate(new Date());
        mapper.insert(notifies);
    }
    //
    //展示所有发布的通知
    @Override
    public R listNotify(int pageSize, int currentPage,int status) {
        PageHelper.startPage(pageSize, currentPage);
        List<Notifies> notifies = mapper.listAll(status);
        notifies.sort(Comparator.comparing(Notifies::getRefreshDate));
        PageInfo<Notifies> notifyPageInfo = new PageInfo<>(notifies);
        PageBean<Notifies> notifyPageBean =
                new PageBean<>(pageSize, currentPage, notifyPageInfo.getTotal(), notifyPageInfo.getList());
        return R.successful("查询成功", notifyPageBean);
    }
    @Override
    public R cacelNotify(int notifyId) {
        int row = mapper.setNotify();
        if (row<=0){
            return R.error("撤销失败", null);
        }else {
            return R.successful("已撤销",null);
        }
    }

    @Override
    public R delete(int notifyId) {
        int remove = mapper.remove(notifyId);
        if (remove<=0){
            return R.error("通知删除失败", null);
        }else {
            return R.successful("通知删除成功", null);
        }
    }

    @Override
    public Notifies editNotify1(Integer notifyId) {
        Notifies notify = mapper.findNotify();
        return notify;
    }
    @Override
    public R editNotify2(Integer notifyId,NotifiesDTO notifiesDTO) {
        Notifies notify = editNotify1(notifyId);
        if (notifiesDTO.getTitle()!=null){
        notify.setTitle(notifiesDTO.getTitle());
    }
        if (notifiesDTO.getContent()!=null){
            notify.setContent(notifiesDTO.getContent());
        }
        if (notifiesDTO.getBookData()!=null){
            notify.setBookDate(notifiesDTO.getBookData());
        }
        notify.setRefreshDate(new Date());
        int i = mapper.editNotify(notify);
        if (i<=0){
            return R.error("修改失败，请重新尝试", null);
        }else
            return R.successful("修改成功，已发布", null);
    }



}
